﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="90px">
        <el-form-item :label="$t('rule.adName')+' :'">
          <el-input
            v-model="listQuery.ad_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('rule.adName')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('video.name')+' :'">
          <el-input
            v-model="listQuery.ad_video_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('video.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('equipment.code')+' :'">
          <el-input
            v-model="listQuery.equipment_code"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('equipment.code')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="90px">
        <el-form-item :label="$t('merchant.name')+' :'">
          <el-input
            v-model="listQuery.merchant_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('merchant.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.region')+' :'">
          <el-input
            v-model="listQuery.region_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.region')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.trade')+' :'">
          <el-input
            v-model="listQuery.trade_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.trade')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('equipment.playTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startPlayTime"
              :picker-options="pickerOptionsB"
              type="date"
              :placeholder="$t('equipment.startPlayTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endPlayTime"
              :picker-options="pickerOptionsE"
              type="date"
              :placeholder="$t('equipment.endPlayTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fixed="left"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.code')">
        <template slot-scope="scope">
          <span>{{ scope.row.equipment_code }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('rule.code')">
        <template slot-scope="scope">
          <span>{{ scope.row.rule_code }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('video.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.ad_video_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('rule.adName')">
        <template slot-scope="scope">
          <span>{{ scope.row.ad_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('merchant.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.merchant_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.region')">
        <template slot-scope="scope">
          <span>{{ scope.row.equipment_region_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.trade')">
        <template slot-scope="scope">
          <span>{{ scope.row.equipment_trade_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('public.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('equipment.sum')">
        <template slot-scope="scope">
          <span>{{ scope.row.sum }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form :model="temp" label-position="right" label-width="30%">
        <el-form-item :label="$t('equipment.code')+' :'">
          <span>{{ temp.equipment_code }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.code')+' :'">
          <span>{{ temp.rule_code }}</span>
        </el-form-item>
        <el-form-item :label="$t('video.name')+' :'">
          <span>{{ temp.ad_video_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.adName')+' :'">
          <span>{{ temp.ad_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('merchant.name')+' :'">
          <span>{{ temp.merchant_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.region')+' :'">
          <span>{{ temp.equipment_region_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.trade')+' :'">
          <span>{{ temp.equipment_trade_name }}</span>
        </el-form-item>
        <el-form-item v-if="temp.interactive" :label="$t('rule.interactMessage')+' :'">
          <span>{{ temp.interactive }}</span>
        </el-form-item>
        <el-form-item v-if="temp.interactive_times" :label="$t('rule.interactNumber')+' :'">
          <span>{{ temp.interactive_times }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.youngCount')+' :'">
          <span>{{ temp.young_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.middleCount')+' :'">
          <span>{{ temp.middle_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.oldCount')+' :'">
          <span>{{ temp.old_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.maleCount')+' :'">
          <span>{{ temp.male_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.femaleCount')+' :'">
          <span>{{ temp.female_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.maxCount')+' :'">
          <span>{{ temp.max_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('rule.minCount')+' :'">
          <span>{{ temp.min_count }}</span>
        </el-form-item>
        <el-form-item :label="$t('equipment.sum')+' :'">
          <span>{{ temp.sum }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "PlayLog",
  data() {
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      opreaterItem: 1,
      listQuery: {
        page: 1,
        limit: 20,
        ad_name: "",
        equipment_code: "",
        region_name: "",
        trade_name: "",
        ad_video_name: "",
        startPlayTime: "",
        endPlayTime: "",
        merchant_name: ""
      },
      temp: {
        id: undefined,
        pid: "",
        name: "",
        linkman: "",
        linkman_mobile: "",
        region_id: "",
        region_name: "",
        region_type: ""
      },
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      loading: false,
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      }
    };
  },
  computed: {
    operWidth: function() {
      return this.opreaterItem * 80 + "px";
    }
  },
  created() {
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endPlayTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endPlayTime).getTime() +
            24 * 3600 * 1000 -
            8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startPlayTime) {
        return (
          time.getTime() <
          new Date(this.listQuery.startPlayTime).getTime() - 8.64e6
        );
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.ad_name) {
        data["ad_name"] = this.listQuery.ad_name;
      }
      if (this.listQuery.equipment_code) {
        data["equipment_code"] = this.listQuery.equipment_code;
      }
      if (this.listQuery.region_name) {
        data["region_name"] = this.listQuery.region_name;
      }
      if (this.listQuery.trade_name) {
        data["trade_name"] = this.listQuery.trade_name;
      }
      if (this.listQuery.ad_video_name) {
        data["ad_video_name"] = this.listQuery.ad_video_name;
      }
      if (this.listQuery.merchant_name) {
        data["merchant_name"] = this.listQuery.merchant_name;
      }
      if (this.listQuery.playTime) {
        data["startPlayTime"] = this.listQuery.playTime[0];
        const date = new Date(this.listQuery.playTime[1]);
        data["endPlayTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      if (this.listQuery.startPlayTime) {
        data["startPlayTime"] = this.listQuery.startPlayTime;
      }
      if (this.listQuery.endPlayTime) {
        const date = new Date(this.listQuery.endPlayTime);
        data["endPlayTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      const adPlayLogIndexPath = {
        // 获取智能广告播放日志列表
        company: "STQ",
        api: "pc_AdPlayLog_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(adPlayLogIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    resetArea() {
      this.areaOptions = [];
      this.temp.region_id = "";
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        ad_name: "",
        equipment_code: "",
        region_name: "",
        trade_name: "",
        ad_video_name: "",
        startPlayTime: "",
        endPlayTime: ""
      };
      this.getList();
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_AdPlayLog_index") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          }
        }
      }
    }
  }
};
</script>
